package com.personalkeeper;

import java.util.ArrayList;
import java.util.Collection;

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

@SuppressWarnings("rawtypes")
public class MarkersLayer extends ItemizedOverlay {

    public interface MarkerTapListener {
        public void onMarkerTap(Marker marker);
    }

    public MarkersLayer(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
        this.populate();
    }

    public void addOverlayRange(Collection<Marker> items) {
        mOverlays.addAll(items);
        setLastFocusedIndex(-1);
        populate();
        
    }

    public void addOverlay(Marker item) {
        mOverlays.add(item);
        setLastFocusedIndex(-1);
        populate();
        
    }
    @Override
    protected OverlayItem createItem(int i) {
        if (i < this.size())
            return mOverlays.get(i);
        else
            return null;
    }

    @Override
    public int size() {
        return mOverlays.size();
    }

    @Override
    protected boolean onTap(int index) {
        indexTapped = index;
        return true;
    }
    @Override
    public boolean onTap(GeoPoint p, MapView mapView) {
        boolean result = false;
        try {
         result = super.onTap(p, mapView);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public void clear() {
        mOverlays.clear();
    }
    public void remove(int index) {
        if (mOverlays.size() > index)
        {
            mOverlays.remove(index);
            setLastFocusedIndex(-1);
            populate();
        }
    }
    public void remove(Object obj) {
        if (mOverlays.contains(obj))
        {
            mOverlays.remove(obj);
            setLastFocusedIndex(-1);
            populate();
        }
    }
    public Marker get(int index) {
        if (this.size() > index)
            return mOverlays.get(index);
        else
            return null;
    }
    public ArrayList<Marker> getItems() {
        return this.mOverlays;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) {
        handleLongpress(event, mapView);
        return super.onTouchEvent(event, mapView);
    }

    private void handleLongpress(final MotionEvent event, final MapView view) {
        if (event.getPointerCount() > 1) {
            // This is a multitouch event, probably zooming.
            handlingTouch = false;
        }
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // Finger has touched screen.
            actionDownTime = event.getEventTime();
            handlingTouch = true;
            break;

        case MotionEvent.ACTION_UP:
            if (handlingTouch) {
                GeoPoint TouchPoint = view.getProjection().fromPixels((int)event.getX(),
                                      (int)event.getY());
                //handling long touch event
                if ((event.getEventTime() - actionDownTime) > LONGPRESS_TIMESPAN) {
                    if (onTap(TouchPoint, view)) {
                        if (indexTapped >= 0) {
                            Marker marker = (Marker) this.getItem(indexTapped);
                            if (marker != null) {
                                int markerType = marker.GetType();
                                if (markerType == Marker.TYPE_CONTACT_POINT ||
                                        markerType == Marker.TYPE_LOCATION_POINT) {                                    
                                    Rect redrawRectangle = marker.getMarker(0).getBounds();
                                    this.remove(indexTapped);
                                    setLastFocusedIndex(-1);
                                    populate();
                                    view.invalidate(redrawRectangle);
                                }
                            }
                            indexTapped = -1;
                        }
                    }
                } else { //handling tap event
                    try {
                        if (onTap(TouchPoint, view)) {
                            if (indexTapped >= 0) {
                                Marker item = (Marker) this.getItem(indexTapped);
                                if (item != null) {
                                    if (markerTapListener == null) {                                        
                                        Toast.makeText(item.getAssotiatedContext(),
                                                       item.getTitle(),
                                                       Toast.LENGTH_SHORT).show();
                                    } else {                                       
                                        markerTapListener.onMarkerTap(item);
                                    }
                                }
                                indexTapped = -1;
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                handlingTouch = false;
            }
            break;

        default:
            handlingTouch = false;
            break;
        }
    }

    public void setOnTapListener(MarkerTapListener listener) {
        markerTapListener = listener;
    }
    private ArrayList<Marker> mOverlays = new ArrayList<Marker>();
    private long actionDownTime;
    private boolean handlingTouch = false;
    private int indexTapped = -1;
    static final int LONGPRESS_TIMESPAN = 500;
    private MarkerTapListener markerTapListener;

}
